<template>
  <div class="versionUpgradManagement">
    <h3>版本升级管理</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入用户名" v-model="search1">
          <template slot="prepend">用户名</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>平台类型</label>
        <el-select v-model="search2" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="opration">
      <el-button type="success" icon="el-icon-plus" @click="addData" v-if="permissions_obj.id_102||permissions_obj.id_all">新增</el-button>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="title" label="标题" width="70"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="version_type_text"
          label="版本类型"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="version_code"
          label="版本号"
          width="70"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="remark" label="备注说明"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="force_text"
          label="强制更新"
          width="80"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="package_path" label="包下载链接地址"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="status_text"
          label="状态"
          width="60"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="创建时间"
          sortable
          width="180"
        ></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)" v-if="permissions_obj.id_103||permissions_obj.id_all">编辑</el-button>
            <el-button size="mini" type="success" @click="handleDelete(scope.$index, scope.row)" v-if="permissions_obj.id_104||permissions_obj.id_all">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
     <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- //修改版本信息弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="modifyEditorDialogVisible" width="40%" center>
      <div class="row">
        <span>版本标题：</span>
        <el-input v-model="inputVersionTitle" placeholder="请输入版本标题" clearable></el-input>
      </div>
      <div class="row">
        <span>平台类型：</span>
        <el-select v-model="inputUserType" placeholder="请选择">
          <el-option
            v-for="item in options5"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="row" v-if="inputUserType=='A'">
        <span>版本类型：</span>
        <span>免费版</span>
      </div>
      <div class="row" v-else>
        <span>版本类型：</span>
        <el-select v-model="inputVersionType" placeholder="请选择">
          <el-option
            v-for="item in options4"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="row">
        <span>版本号：</span>
        <el-input v-model="inputVersionNum" placeholder="请输入广告简介" clearable></el-input>
      </div>
      <div class="row">
        <span>是否强制更新：</span>
        <el-select v-model="inputIsUpdate" placeholder="请选择">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="row">
        <span>包地址：</span>
        <el-input v-model="inputAdress" placeholder="请输入包地址" clearable></el-input>
      </div>
      <div class="row">
        <span>状态：</span>
        <el-select v-model="inputStatus" placeholder="请选择">
          <el-option
            v-for="item in options3"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="row">
        <span>备注说明：</span>
        <el-input v-model="inputNote" placeholder="请输入备注说明" clearable type="textarea" :rows="3"></el-input>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitModifyEditor">提交</el-button>
        <el-button type="info" size="small" @click="cancelModifyEditor">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "versionUpgradManagement",
  data() {
    return {
      options: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "A",
          label: "安卓"
        },
        {
          value: "I",
          label: "IOS"
        }
      ],
      options2: [
        {
          value: 0,
          label: "不是"
        },
        {
          value: 1,
          label: "是"
        }
      ],
      options3: [
        {
          value: 0,
          label: "禁用"
        },
        {
          value: 1,
          label: "启用"
        }
      ],
      options4: [
        {
          value: "free",
          label: "免费版"
        },
        {
          value: "shop",
          label: "商店版"
        },
        {
          value: "testflight",
          label: "防封版"
        }
      ],
      options5: [
        {
          value: "A",
          label: "安卓"
        },
        {
          value: "I",
          label: "IOS"
        }
      ],
      search1: "",
      search2: "",
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      dialogTitle: "修改版本信息",
      modifyEditorDialogVisible: false,
      //弹窗版本标题
      inputVersionTitle: "",
      //弹窗用户类型
      inputUserType: "",
      //弹窗版本类型
      inputVersionType: "",
      //弹窗版本号
      inputVersionNum: "",
      //弹窗是否强制更新
      inputIsUpdate: "",
      //弹窗包地址
      inputAdress: "",
      //弹窗状态
      inputStatus: "",
      //弹窗备注说明
      inputNote: "",
      selectId: "",
      // 权限管理
       permissions:[],
      permissions_obj:{},
    };
  },
  created(){ 
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff=false;
    this.permissions.forEach((item, index) => {
      if(item==-1){
        oneOff=true;
      }else{
        this.permissions_obj['id_'+item]=item;
      }
    });
    if(oneOff){
      this.permissions_obj.id_all=true;
    }else{
      this.permissions_obj.id_all=false;
    };
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getDataList();
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.getDataList();
    },
    //搜索
    searchMethod() {
      this.currentPage1 = 1;
      this.getDataList();
    },
    //新增
    addData() {
      this.inputVersionTitle = "";
      this.inputUserType = "";
      this.inputVersionType = "";
      this.inputVersionNum = "";
      this.inputIsUpdate = "";
      this.inputAdress = "";
      this.inputStatus = "";
      this.inputNote = "";
      this.modifyEditorDialogVisible = true;
      this.dialogTitle = "创建版本信息";
    },
    //编辑
    handleEdit(index, row) {
      this.modifyEditorDialogVisible = true;
      this.dialogTitle = "修改版本信息";
      this.selectId = row.id;
      this.inputVersionTitle = row.title;
      this.inputUserType = row.device_type;
      this.inputVersionType = row.version_type;
      this.inputVersionNum = row.version_code;
      this.inputIsUpdate = row.force;
      this.inputAdress = row.package_path;
      this.inputStatus = row.status;
      this.inputNote = row.remark;
    },
    //删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该行数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteMethod(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //提交修改或者新增
    submitModifyEditor() {
      if (this.dialogTitle == "创建版本信息") {
        this.addMethod();
      } else if (this.dialogTitle == "修改版本信息") {
        this.modifyMethod();
      }
    },
    //取消提交
    cancelModifyEditor() {
      this.modifyEditorDialogVisible = false;
    },
    //获取初始化用户列表
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            title: this.search1,
            device_type: this.search2
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.versionLists,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          if (item.device_type == "I") {
            item.device_text = "苹果";
          } else if (item.device_type == "A") {
            item.device_text = "安卓";
          } else if (item.device_type == "H") {
            item.device_text = "来源h5";
          }
          if (item.force == 0) {
            item.force_text = "不是";
          } else if (item.force == 1) {
            item.force_text = "是";
          }
          if (item.version_type == "free") {
            item.version_type_text = "免费版";
          } else if (item.version_type == "shop") {
            item.version_type_text = "商店版";
          } else if (item.version_type == "testflight") {
            item.version_type_text = "防封版";
          }
          if (item.status == 0) {
            item.status_text = "未启用";
          } else if (item.status == 1) {
            item.status_text = "启用";
          }
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //新增接口
    async addMethod() {
      let parames = {
        handshake: "",
        data: {
          title: this.inputVersionTitle,
          device_type: this.inputUserType,
          version_type: this.inputUserType=='A'?'free':this.inputVersionType,
          version_code: this.inputVersionNum,
          force: this.inputIsUpdate,
          package_path: this.inputAdress,
          status: this.inputStatus,
          remark: this.inputNote
        }
      };
      let res = await this.$axios.post(
        this.$api.versionAdd,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //修改接口
    async modifyMethod() {
      let parames = {
        handshake: "",
        data: {
          id: this.selectId,
          title: this.inputVersionTitle,
          device_type: this.inputUserType,
          version_type: this.inputUserType=='A'?'free':this.inputVersionType,
          version_code: this.inputVersionNum,
          force: this.inputIsUpdate,
          package_path: this.inputAdress,
          status: this.inputStatus,
          remark: this.inputNote
        }
      };
      let res = await this.$axios.post(
        this.$api.versionUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //  删除接口
    async deleteMethod(id) {
      let parames = {
        handshake: "",
        data: {
          id: id
        }
      };
      let res = await this.$axios.post(
        this.$api.versionDelete,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
</style>